package com.cs.framework.common.util;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import org.springframework.core.io.Resource;

/**
 * xml工具类
 * 
 * @author hedongzhou
 *
 */
public class XmlUtils {

	/**
	 * xml转换成对象
	 * 
	 * @param <T>
	 * @param res
	 *            xml源
	 * @param cls
	 *            对象类型
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T toObject(Resource res, Class<T> cls) {
		try {
			JAXBContext context = JAXBContext.newInstance(cls);
			Unmarshaller unmarshaller = context.createUnmarshaller();

			return (T) unmarshaller.unmarshal(res.getFile());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
